<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>
        <label>猜数字：</label>
        <input type="text" placeholder="请输入100以内自然数" />
        <button type="button" id="ok">确定</button>
    </div>
    <label id="prev">上一次才的数字：</label>
    <p id="msg1"></p>
    <p id="msg2"></p>
    <button type="button" id="reset">开始游戏</button>
    <script type="text/javascript">
        const input = document.querySelector('input');
        const btn = document.querySelector('#ok');
        const resetBtn = document.querySelector("#reset");
        let msg1 = document.querySelector("#msg1");
        let msg2 = document.querySelector("#msg2");
        let prev = document.querySelector("#prev");
        const num = Math.floor(Math.random() * 100) + 1;
        resetBtn.style.display = "none";
        prev.style.display = "none";
        let count = 0;
        btn.onclick = function () {
            let val = input.value;
            if (!val || val == "") {
                val = 0;
            }
            console.log(count);
            if (Number(val) === num) {
                input.setAttribute('disabled', 'true');
                btn.setAttribute('disabled', 'true');
                msg1.style.backgroundColor = "green";
                msg1.style.color = "#fff";
                msg1.style.padding = "10px";
                msg1.innerHTML = "猜对了！！！";
                resetBtn.style.display = "block";
            } else {
                if (++count < 10) {
                    msg1.style.backgroundColor = "red";
                    msg1.style.color = "#fff";
                    msg1.style.padding = "10px";
                    Number(val) < num ? msg1.innerHTML = "猜错了,数字小了！！！" : msg1.textContent = "猜错了,数字大了！！！";
                    prev.style.display = "block";
                    let createSpan = document.createElement("span");
                    createSpan.style.display = "inline-block";
                    createSpan.style.padding = "10px";
                    createSpan.textContent = val;
                    prev.appendChild(createSpan);
                    resetBtn.style.display = "none";
                }else{
                    alert("游戏结束");
                }

            }
        }
    </script>
</body>

</html>